#include <jni.h>
#include <string>
#include "common.h"
#include "demuxing_decoding.h"

#ifdef __cplusplus
extern "C"
{
#endif

    #include <libavformat/avformat.h>

#ifdef __cplusplus
};
#endif



void ffmpeg_log_callback(void *ptr, int level, const char *fmt, va_list vl)
{
    va_list vl2;
    char line[1024];
    static int print_prefix = 1;


    va_copy(vl2, vl);
    // av_log_default_callback(ptr, level, fmt, vl);
    av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
    va_end(vl2);

    LOGD("%s", line);

}


extern "C" JNIEXPORT void JNICALL
Java_com_example_king_ffmpegexample_NativeUtil_init(
        JNIEnv* env,
        jobject /* this */) {

    av_register_all();
    av_log_set_callback(ffmpeg_log_callback);

//    unsigned version = avformat_version();
//    const char* license = avformat_license();
//
//    LOGD("ffmpeg version %u, license:%s", version, license);

}

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_king_ffmpegexample_NativeUtil_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}


extern "C" JNIEXPORT void JNICALL
        Java_com_example_king_ffmpegexample_NativeUtil_decodeMedia2Yuv(
                JNIEnv* env,
                jobject /* this */,
                jstring mediaFilePath,
                jstring outputFilePath,
                jstring outputAudioFilePath) {

    simpleDemuxingDecoding(env, mediaFilePath, outputFilePath, outputAudioFilePath);
}

